import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config} from '../../shared/smartadmin.config';

declare var $: any;
declare var echarts: any;
@Component({
	selector: 'sa-event-status',
	templateUrl: './event-status.component.html',
})
export class EventStatusComponent implements OnInit {
	tableData:any= []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize :number= 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	eventType:string = '';//事件类型
	startTime:string=''; //开始时间
	endTime:string=''; //结束时间
	selectedCompanyId:string = ''; //公司ID
	selectedCompanyName:string = ''; //公司名称
	carNumber:string = '';
	deviceId:string = '';
	selfDevieId:string = '';
	constructor(private jsonApiService: JsonApiService, private router: Router,private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage || 1;
		});
	}

	ngOnInit() {
		this.initDate().then(()=>{
		    this.load();
		});
	}
	ngOnDestroy(){}
	initDate(){
		var that = this;
		return new Promise(function (resolve, reject) {
			System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(()=>{
					System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(()=>{
						$("#datetimepicker1").datetimepicker({//时间插件
							format: "yyyy-mm-dd hh:ii:ss",
						    language: "zh-CN",
						    autoclose: true,
						}).on("changeDate", function(e){
							var date = e.date;
							var time = common.getTimeMmss(date);
							$("#datetimepicker2").datetimepicker("setStartDate", time);
							that.startTime=time;
						});
						$("#datetimepicker2").datetimepicker({//时间插件
							format: "yyyy-mm-dd hh:ii:ss",
						    language: "zh-CN",
						    autoclose: true,
						}).on("changeDate", function(e){
							var date = e.date;
							var time = common.getTimeMmss(date);
							$("#datetimepicker1").datetimepicker("setEndDate", time);
							that.endTime=time;
						});
						var date = new Date();
                        let time1 = common.getTimeMmss(date);
                        $("#datetimepicker2").val(time1); //当前时间
                        that.endTime = time1;
                        date.setDate(date.getDate() - 1); //设置前1天
                        let time2 = common.getTimeMmss(date);
                        $("#datetimepicker1").val(time2);
                        that.startTime = time2;
						resolve();
					});
				}
			);
		})

	}
	//清除所选时间
	del_date(){
		this.startTime='';
		this.endTime='';
		$("#datetimepicker1").val('').change();
		$("#datetimepicker2").val('').change();
		$("#datetimepicker2").datetimepicker("setStartDate", null);
		$("#datetimepicker1").datetimepicker("setEndDate", null);
	}
	//表格搜索
	table_search() {
		this.curPage=1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//刷新
	refresh(){
    	this.load();
   	}
	//初始化加载list数据
	load() {
		this.startTime =$('#datetimepicker1').val();
		this.endTime=$('#datetimepicker2').val();
		if(this.startTime==''&&this.endTime!==''){
			this.jsonApiService.message('起始时间不能为空');
			return;
		}
		if(this.startTime!==''&&this.endTime==''){
			this.jsonApiService.message('结束时间不能为空')
			return;
		}
		if(this.startTime!==''&& !common.checkDateFormat(this.startTime)){
			this.jsonApiService.message('起始时间格式错误')
			return;
		}
		if(this.endTime!==''&& !common.checkDateFormat(this.endTime)){
			this.jsonApiService.message('结束时间格式错误')
			return;
		}
		
		let reqdata = 'gridPager={"pageSize":' + this.pageSize +',"nowPage":' + this.curPage + ',"parameters":{"searchKey":"' + this.searchKey +
			'",startTime:"'+ this.startTime +
			'",endTime:"' + this.endTime +
			'",companyId:"' + this.selectedCompanyId +
			'",deviceId:"' + this.deviceId +
			'",type:"' + this.eventType +
		'"}}'
		this.jsonApiService.http("logForDeviceStatus/queryLogForDeviceStatusList.html", reqdata).subscribe(data => {
			if(data.isSuccess){
				var result = data.exhibitDatas;
				this.tableData = result;
				this.totalCount = data.recordCount;
			}
		})
	}
	///分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//删除
	del_record(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("deviceInstall/delDeviceInstallById.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message)
				this.load();
			})
		})
	}
	//公司选中回调
	companySelected(data){
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
		this.deviceId = data.deviceId;
		this.carNumber = data.carNumber;
	}
}